package com.netflexitysolutions.amazonws.ec2.internal.operations;

import java.util.List;
import java.util.Set;

import com.amazonaws.ec2.doc._2009_04_04.DescribeRegionsResponseType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeRegionsSetItemType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeRegionsSetType;
import com.amazonaws.ec2.doc._2009_04_04.DescribeRegionsType;
import com.netflexitysolutions.amazonws.ec2.EC2;
import com.netflexitysolutions.amazonws.ec2.Region;
import com.netflexitysolutions.amazonws.ec2.internal.ModelUtil;

public class DescribeRegionsOperation extends ObjectSetOperationExecutor<DescribeRegionsType, List<Region>> {
	
	public DescribeRegionsOperation(EC2 ec2, Set<String> regionNames) {
		super(ec2, regionNames);
	}

	@Override
	protected List<Region> call(DescribeRegionsType request) {
		request.setRegionSet(createRegionNamesElement());

		DescribeRegionsResponseType response = getEC2().getService().describeRegions(request);

		return ModelUtil.toDomainType(response.getRegionInfo());
	}

	private DescribeRegionsSetType createRegionNamesElement() {
		DescribeRegionsSetType element = new DescribeRegionsSetType();
		List<DescribeRegionsSetItemType> list = element.getItem();
		if (objectIds != null) {	
			for (String regionName : objectIds) {
				DescribeRegionsSetItemType item = new DescribeRegionsSetItemType();
				item.setRegionName(regionName);
				list.add(item);
			}
		}
		return element;
	}

}
